-- LFG_Logger.lua
local f = CreateFrame("Frame", "LFGLoggerFrame", UIParent)
f:SetSize(300, 200)
f:SetPoint("CENTER")
f:Hide()

-- 兼容5.5版本的背景创建（使用纹理代替SetBackdrop）
local bg = f:CreateTexture(nil, "BACKGROUND")
bg:SetColorTexture(0, 0, 0, 0.7) -- 黑色背景，70%透明度
bg:SetAllPoints(f)

-- 创建边框（四边分开创建）
local borderPoints = {
    {point = "TOPLEFT",    width = 1, height = 0,   offset = 0, vertical = false},
    {point = "TOPRIGHT",   width = 1, height = 0,   offset = 0, vertical = false},
    {point = "BOTTOMLEFT", width = 1, height = 0,   offset = 0, vertical = false},
    {point = "BOTTOMRIGHT",width = 1, height = 0,   offset = 0, vertical = false},
    {point = "TOP",        width = 0, height = 1,   offset = 0, vertical = true},
    {point = "BOTTOM",     width = 0, height = 1,   offset = 0, vertical = true},
    {point = "LEFT",       width = 0, height = 1,   offset = 0, vertical = true},
    {point = "RIGHT",      width = 0, height = 1,   offset = 0, vertical = true}
}

for _, bp in ipairs(borderPoints) do
    local border = f:CreateTexture(nil, "BORDER")
    border:SetColorTexture(0.4, 0.4, 0.4) -- 深灰色边框
    if bp.vertical then
        border:SetPoint(bp.point, f, bp.point, bp.offset, 0)
        border:SetHeight(bp.height)
        border:SetWidth(f:GetWidth())
    else
        border:SetPoint(bp.point, f, bp.point, 0, bp.offset)
        border:SetWidth(bp.width)
        border:SetHeight(f:GetHeight())
    end
end

-- 滚动框架（使用经典怀旧服兼容模板）
local scroll = CreateFrame("ScrollFrame", "LFGLoggerScroll", f, "UIPanelScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", 8, -28)
scroll:SetPoint("BOTTOMRIGHT", -28, 8)

local edit = CreateFrame("EditBox", nil, scroll)
edit:SetMultiLine(true)
edit:SetFontObject("GameFontNormal")
edit:SetWidth(264)
edit:SetMaxLetters(99999)
edit:SetAutoFocus(false)
edit:SetText("寻求组队信息将显示在这里...")
scroll:SetScrollChild(edit)

f.scroll = scroll
f.edit = edit

-- 标题
f.title = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
f.title:SetPoint("TOP", 0, -8)
f.title:SetText("寻求组队信息记录器")

-- 按钮（使用经典模板）
f.toggle = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
f.toggle:SetSize(80, 22)
f.toggle:SetPoint("BOTTOM", 0, 8)
f.toggle:SetText("隐藏")
f.toggle:SetScript("OnClick", function() f:Hide() end)

-- 数据存储
local LFGData = {}

-- 版本兼容的频道名称获取
local LFG_CHANNEL_NAME
do
    local realms = { "寻求组队", LOOKING_FOR_GROUP }
    for _, name in ipairs(realms) do
        if name then
            for i=1, 10 do
                local chName = GetChannelName(i)
                if chName == name then
                    LFG_CHANNEL_NAME = name
                    break
                end
            end
            if LFG_CHANNEL_NAME then break end
        end
    end
    LFG_CHANNEL_NAME = LFG_CHANNEL_NAME or "寻求组队"
end

-- 事件处理
f:RegisterEvent("CHAT_MSG_CHANNEL")
f:SetScript("OnEvent", function(self, event, text, playerName, _, _, _, _, _, channelID, ...)
    local channelName = GetChannelName(channelID)

    if not channelName then
        -- 兼容旧版本
        local _, channelName = GetChannelList()
    end

    if channelName == LFG_CHANNEL_NAME then
        -- 获取玩家职业和等级（兼容方法）
        local _, class = UnitClass(playerName)
        local level = UnitLevel(playerName) or 0
        local color = RAID_CLASS_COLORS[class] or {r=1, g=1, b=1}

        -- 添加到数据表
        table.insert(LFGData, {
            name = playerName,
            class = class,
            level = level,
            message = text,
            timestamp = date("%H:%M:%S"),
            color = color
        })

        -- 更新显示
        UpdateDisplay()
    end
end)

-- 更新显示内容
function UpdateDisplay()
    local displayText = ""
    for i, data in ipairs(LFGData) do
        displayText = displayText .. format("|cff%02x%02x%02x%s|r [%d] %s: |cffffffff%s|r\n",
                data.color.r*255, data.color.g*255, data.color.b*255,
                data.name, data.level, data.timestamp, data.message)
    end
    f.edit:SetText(displayText)
    f.edit:HighlightText(0,0)  -- 取消自动选择
    f.scroll:UpdateScrollChildRect()

    -- 滚动到底部（兼容方法）
    local maxScroll = f.scroll:GetVerticalScrollRange()
    f.scroll:SetVerticalScroll(maxScroll)
end

-- 命令系统
SLASH_LFGLOGGER1 = "/lfglogger"
SLASH_LFGLOGGER2 = "/lfg"
SlashCmdList["LFGLOGGER"] = function()
    if f:IsShown() then
        f:Hide()
    else
        f:Show()
        if #LFGData > 0 then
            UpdateDisplay()
        else
            f.edit:SetText("尚未记录到寻求组队信息...")
        end
    end
end

print("|cFF00FF00LFG Logger 已加载|r 输入 |cFFFFFF00/lfg|r 显示窗口")